package com.gk.springauth2order.controller;

import com.gk.springauth2common.entity.UserDO;
import com.gk.springauth2order.sevice.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 提供订单服务的入口
 * @Author poison
 * @Date 2020/10/14 11:13
 */
@RestController
public class OrderController {

    @Autowired
    private EmailService emailService;

    @GetMapping(value = "/list")
    //拥有p1权限方可访问此url
    @PreAuthorize("hasAnyAuthority('p1')")
    public String testR1(){
        //获取用户身份信息
        String principal = (String)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return principal+"展示商品";
    }

    @GetMapping(value = "/order")
    //拥有p1权限方可访问此url
    @PreAuthorize("hasAnyAuthority('p1')")
    public String testR2(){
        String str = null;
        try{
            str = emailService.emailTest();
        }catch (Exception e){
            e.printStackTrace();
        }
        if(str==null){
            str = "无法调用邮件服务，请检查服务配置";
        }
        return str;
    }

    @GetMapping(value="/a/a1")
    public String testA1(){
        return "不用验证访问资源";
    }

}
